(function(window, angular, undefined){
	angular.module('gis.toolbar', [])
	.factory('$gis_toolbar', ["$rootScope", function($rootScope) {
		$gis_toolbar = {

			// GIS全屏
			gisFullextent:function(){
				var go = $rootScope.gisObject;
				if(go.is2D==true){
					$rootScope.mapFullextentEx();
				}else{
					$rootScope.cesiumFullextent();
				}
			}
		};
		for(f in $gis_toolbar){
			$rootScope[f] = $gis_toolbar[f];
		}
		return $gis_toolbar;
	}])
	.run(["$rootScope", "$gis_toolbar", function($rootScope, $gis_toolbar) {
	}]);
}(window, angular));
(function(window, angular, undefined){
	angular.module('gis.locator', [])
	.factory('$gis_locator', ["$rootScope", function($rootScope) {
		$gis_locator = {

			// 一个特定方法，根据名称，返回特定级别
			getZqLevel : function(name){
				if(!name) return 6;
				if(name.endWith('省') || name.endWith('自治区')){
					return 6;
				}
				if(name.endWith('市') || name.endWith('地区')){
					return 8;
				}
				if(name.endWith('县')){
					return 10;
				}
				if(name.endWith('乡') || name.endWith('镇') || name.endWith('村')){
					return 10;
				}
			},

			// 定位到指定名称的地址
			addressSuperLocator : function(name, level){
				if(!level) level = this.getZqLevel(name);
				var go = $rootScope.gisObject;
				if(go.is2D==true){
					$rootScope.mapAddressLocator(name, level);
				}else{
					$rootScope.cesiumAddressLocator(name, level);
				}
			},

			/* 
			 * 经纬度定位
			 */
			jwdSuperLocator : function(){
				var go = $rootScope.gisObject;
				var lonD = parseInt(go.locPara.jdd);
				var lonF = parseInt(go.locPara.jdf);
				var lonM = parseFloat(go.locPara.jdm);
				var latD = parseInt(go.locPara.wdd);
				var latF = parseInt(go.locPara.wdf);
				var latM = parseFloat(go.locPara.wdm);
				
				if(go.is2D==true){
					$rootScope.mapJwdLocator(lonD,lonF,lonM,latD,latF,latM);
				}else{
					$rootScope.cesiumJwdLocator(lonD,lonF,lonM,latD,latF,latM);
				}
			},

			/* 
			 * 地理坐标定位
			 */
			dlSuperLocator : function(){
				var go = $rootScope.gisObject;
				var x = parseFloat(go.locPara.dlx);
				var y = parseFloat(go.locPara.dly);

				if(go.is2D==true){
					$rootScope.mapDlLocator(x,y);
				}else{
					$rootScope.cesiumDlLocator(x,y);
				}
			},

			/* 
			 * 平面坐标定位
			 */
			pmSuperLocator : function(){
				var go = $rootScope.gisObject;
				var x = parseFloat(go.locPara.pmx);
				var y = parseFloat(go.locPara.pmy);

				if(go.is2D==true){
					$rootScope.mapPmLocator(x,y);
				}else{
					$rootScope.cesiumPmLocator(x,y);
				}
			}
		};
		for(f in $gis_locator){
			$rootScope[f] = $gis_locator[f];
		}
		return $gis_locator;
	}])
	.run(["$rootScope", "$gis_locator", function($rootScope, $gis_locator) {
	}]);
}(window, angular));
(function(window, angular, undefined){
	// 定义安全框架对外接口
	angular.module('gis.config', [])
	.factory('$gis_config', ["$rootScope", function($rootScope) {
		var $gis_config = {

			// 初始化当前用户和权限信息
			initMapConfig : function(){
				var locatorService = getGisBaseUrl()+"/FindLocator/GeocodeServer";
				var geometryService = getGisBaseUrl()+"/Utilities/Geometry/GeometryServer";
				var printingService = getGisBaseUrl()+"/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task";

				$rootScope.initLocatorService(locatorService);
				$rootScope.initGeometryService(geometryService);
				$rootScope.initPrintingService(printingService);

				var go = $rootScope.gisObject;
				var iniAll = function(){
					if(go.map && go.LayerConfig){
						$rootScope.initMayLayer(go.LayerConfig);
					}
				};

				// 获取图层配置
				if(go.LayerConfig){
					iniAll();
				}else{
					$gis_config.getLayerConfig(
						function(data, textStatus, xOptions){
							go.LayerConfig = data;
							iniAll();
						},
						function(xhr, status, data){}
					);
				}
			},

			// 初始化当前用户和权限信息
			initCesiumConfig : function(){
				var go = $rootScope.gisObject;
				var iniAll = function(){
					if(go.cesium && go.LayerConfig){
						$rootScope.initCesiumLayer(go.LayerConfig);
					}
				};

				// 获取图层配置
				if(go.LayerConfig){
					iniAll();
				}else{
					$gis_config.getLayerConfig(
						function(data, textStatus, xOptions){
							go.LayerConfig = data;
							iniAll();
						},
						function(xhr, status, data){}
					);
				}
			},

			// 图层配置
			getLayerConfig : function(onsuccess, onerror) {
				$.ajax({
					url : getServerBaseUrl()+'/cgislayerconfig/getLayerConfig',
					type : 'POST',
					data : null,
					timeout : 35000,
					headers: { 'jwt': $.cookie('jwt') },
					dataType : 'json',
					success : onsuccess,
					error : onerror
				});
			}
		};
		for(f in $gis_config){
			$rootScope[f] = $gis_config[f];
		}
		return $gis_config;
	}])
	.run(["$rootScope", "$gis_config", function($rootScope, $gis_config) { }]);
}(window, angular));
(function(window, angular, undefined){
	angular.module('gis', ['gis.toolbar','gis.locator','gis.config'])
	.factory('$gis', ["$rootScope", function($rootScope) {
		$gis = {

			gisObject : {
				is2D:true
			},

			// 切换地图状态 2d - 3d
			setMapState:function(is2D){
				var go = $rootScope.gisObject;
				if(is2D==null){
					is2D = !go.is2D;
				}else{
					if(go.is2D==is2D) return;
				}

				go.is2D = is2D;
				if(is2D){
					$rootScope.direct2Dmap();
				}else{
					$rootScope.direct3Dmap();
				}
			},

			/**
			 * 二维定位到三维问题
			 * @return {[type]} [description]
			 */
			direct3Dmap:function(){
				var go = $rootScope.gisObject;
				$rootScope.setCesiumExtent(go.map.extent);
			},

			/**
			 * 三维定位2维
			 * @return {[type]} [description]
			 */
			direct2Dmap:function(){
				var go = $rootScope.gisObject;
				var json_extent=$rootScope.getCurrent3DExtent();
				var wkid = null;
				if(go.mapisdlwkid){
					wkid = go.dlwkid;
				}else{
					wkid = go.tywkid;
				}

				var contain=true;
				if(go.fullExtent!=null){
					var v1=true;
					var v2=true;
					var v3=true;
					var v4=true;

					if(json_extent.xmin==null || json_extent.xmin<go.fullExtent.xmin){
						v1=false;
					}

					if(json_extent.xmax==null || json_extent.xmax>go.fullExtent.xmax){
						v2=false;
					}

					if(json_extent.ymin==null || json_extent.ymin<go.fullExtent.ymin){
						v3=false;
					}

					if(json_extent.ymax==null || json_extent.ymax>go.fullExtent.ymax){
						v4=false;
					}

					if(!v1 && !v2 && !v3 && !v4) contain = false;
				}
				if(contain==false){
					go.map.setExtent(go.fullExtent);
				}else{
					var extent = new esri.geometry.Extent({
						"xmin" : parseFloat(json_extent.xmin),
						"ymin" : parseFloat(json_extent.ymin),
						"xmax" : parseFloat(json_extent.xmax),
						"ymax" : parseFloat(json_extent.ymax),
						"spatialReference" : wkid
					});
					go.map.setExtent(extent);
				}
			}
		};
		for(f in $gis){
			$rootScope[f] = $gis[f];
		}
		return $gis;
	}])
	.run(["$rootScope", "$gis", function($rootScope, $gis) {
	}]);
}(window, angular));